<?php

namespace Event\Drive\Facades;

use Event\Drive\Exceptions\EventDriveException;
use Event\Drive\Service\EventDriveService;
use Event\Drive\Service\RegisterEventManageService;

abstract class EventDriveClient
{
    /**
     * @var EventDriveService
     */
    private $eventDriveService;

    /**
     * @var RegisterEventManageService
     */
    private $registerEventManageService;

    public function __construct()
    {
        $this->eventDriveService = $this->getEventDriveService();
        $this->registerEventManageService = $this->getRegisterEventManageService();
    }

    /**
     * 注册Event-Task
     * @param $eventClassName
     * @param array $params
     * @return void
     * @throws \Event\Drive\Exceptions\EventDriveException
     */
    public function registerEventTask($eventClassName, array $params)
    {
        //检验事件是否注册
        if (!$this->registerEventManageService->eventExists($eventClassName)) {
            throw new EventDriveException($eventClassName,'未注册');
        }
        $this->eventDriveService->registerEventTask($eventClassName, $params);
    }


    /**
     * 执行Event-Task
     * @return void
     */
    public function executeEventTask()
    {
        $this->eventDriveService->execute();
    }

    /**
     * 注册事件
     * @param string $eventClassName
     * @return void
     * @throws \Event\Drive\Exceptions\EventDriveException
     */
    public function registerEvent(string $eventClassName) : void
    {
        $this->registerEventManageService->register($eventClassName);
    }

    /**
     * 注销事件
     * @param string $eventClassName
     * @return void
     * @throws \Event\Drive\Exceptions\EventDriveException
     * @throws \Throwable
     */
    public function unregisterEvent(string $eventClassName) : void
    {
        $this->registerEventManageService->unregister($eventClassName);
    }


    /**
     * 获取已注册事件列表
     * @return array
     * @throws \Event\Drive\Exceptions\EventDriveException
     */
    public function getEventList() : array
    {
        return $this->registerEventManageService->getEventList();
    }

    abstract protected function getEventDriveService() : EventDriveService;

    abstract protected function getRegisterEventManageService() : RegisterEventManageService;
}
